const http = require("http");
const fs = require("fs")
const urls = require("url")
const utils = require("./util.js")

// console.log(modulea);
    const server = http.createServer(function(req,res){
    // console.log(1111);
    let pathObj = urls.parse(req.url,true)
    let query = pathObj.query
    let url
    
    if(typeof query.path !="undefined"){
        url=query.path
    }else{
        url="./test"
    }
    if(typeof query.del !="undefined"){
        let delpath =query.del
        let stats = fs.statSync(delpath)
        if(stats.isDirectory()){
            utils.removeDir(delpath)
        }else{
            fs.unlinkSync(delpath)
        }
    }


    res.setHeader("Content-Type","text/html;charset=utf-8")
    let html ="<table>"
    let arr = fs.readdirSync(url)
    arr.forEach(function(item){     
       let st = fs.statSync(url+"/"+item)
       if(st.isDirectory()){
            html+=`<tr><td><a>${item}</a></td></tr>`
       }else{
           html+=`<tr><td>${item}</td><td><a>删除</a></td></tr>`
       }
    })
    html+="</table>"
    res.write(html)
    res.end()
    
})

server.listen(9898)
